<?php
/**
 * ============================================================================
 * 版权所有 2017-2077 tpframe工作室，并保留所有权利。
 * @link http://www.tpframe.com/
 * @copyright Copyright (c) 2017 TPFrame Software LLC
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！未经本公司授权您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 */
namespace app\frontend\controller;
use \tpfcore\Core;
use app\frontend\model\SlideCat;
use app\frontend\model\Slide;
use think\facade\Cookie;
use think\facade\Db;
use think\facade\View;
use think\Url;
class Index extends FrontendBase
{
    public function index()
    {   
    	$listIndexProduct = Core::loadModel("Posts")->getTpfPosts([
            // "where"=>["cateid"=>["in",["1","2","3","4","5"]],"isdelete"=>0,"ischeck"=>1],
            "where"=>["cateid"=>["1","2","3","4","5"],"isdelete"=>0,"ischeck"=>1],
            "field"=>"id,title,thumb",
            "limit"=>8
        ]);
        $listBzProduct = Core::loadModel("Posts")->getTpfPosts([
            "where"=>["cateid"=>3,"isdelete"=>0,"ischeck"=>1],
            "field"=>"id,title,thumb",
            "limit"=>4
        ]);
        $listIndexNews = Core::loadModel("Posts")->getTpfPosts([
            "where"=>["cateid"=>7,"isdelete"=>0,"ischeck"=>1],
            "field"=>"id,title,content,datetime",
            "limit"=>6
        ]);
        return $this->fetch("index",[
            "listIndexProduct"=>$listIndexProduct,
            "listBzProduct"=>$listBzProduct,
            "listIndexNews"=>$listIndexNews
        ]);
    }

    public function product(){
        $cid = isset($this->param['cid'])?$this->param['cid']:1;

        // 通过父id查询所有子ID，包括后代的后代
        $ids = Core::loadModel('Category')->getChildIds($cid,true);

        $where = ["cateid"=>$ids,'isdelete'=>0,'ischeck'=>1];

        // 查询分类，获取SEO相关信息
        $currCategory = Core::loadModel("Category")->getOneObject(['id'=>$cid]);

        if($currCategory){
            $this->seo_title = $currCategory->seo_title?:$currCategory->title;
            $this->seo_keywords = $currCategory->seo_keywords;
            $this->seo_description = $currCategory->seo_description;
        }

        $list = Core::loadModel("Posts")->getTpfPosts([
            "where"=>$where,
            "field"=>"id,title,thumb,parentid",
            "paginate"  =>['rows' =>12],
        ]);

        // 面包屑
        $bread = '<li class=home><a href="/">首页</a></li>';

        if(isset($this->param['cid'])){
            $listCategory = Core::loadModel("Category")->getCategory([
                'where'=>['id'=>$this->param['cid']],
                "field"=>'id,title,parentid'
            ]);

            if($listCategory[0]){

                $parentCategory = Core::loadModel("Category")->getColumnValue(['id'=>$listCategory[0]->parentid],"title");

                $bread.='<li class=active>
                            <i class="wpcom-icon wi">
                              <svg aria-hidden=true>
                                <use xlink:href=#wi-arrow-right-3></use>
                              </svg>
                            </i>
                            <a href="'.url('index/product').'">'.$parentCategory.'</a>
                          </li>';
                $bread.='<li class=active>
                            <i class="wpcom-icon wi">
                              <svg aria-hidden=true>
                                <use xlink:href=#wi-arrow-right-3></use>
                              </svg>
                            </i>
                            <a href="javascript:;">'.$listCategory[0]->title.'</a>
                          </li>';
            }
        }else{
            $listCategory = Core::loadModel("Category")->getCategory([
                'where'=>['id'=>1],
                "field"=>'id,title,parentid'
            ]);
            if($listCategory[0]){
                $bread.='<li class=active>
                            <i class="wpcom-icon wi">
                              <svg aria-hidden=true>
                                <use xlink:href=#wi-arrow-right-3></use>
                              </svg>
                            </i>
                            <a href="'.url('index/product').'">'.$listCategory[0]->title.'</a>
                          </li>';
            }
        }

        return $this->fetch("product",["list"=>$list,"bread"=>$bread]);
    }
    public function product_detail(){
        if(empty($this->param['id'])){
            $this->jump([RESULT_ERROR,'非法访问']);
        }

        $list = Core::loadModel("Posts")->getOneObject(["id"=>$this->param['id'],"isdelete"=>0,"ischeck"=>1],"id,title,abstract,keywords,content,parentid,cateid,datetime");

        if($list){
            $this->seo_title = $list->title;
            $this->seo_keywords = $list->keywords;
            $this->seo_description = $list->abstract;
        }else{
            $this->jump([RESULT_ERROR,'产品不存在']);
        }

        // 面包屑
        $bread = '<li class=home><a href="/">首页</a></li>';

        $listCategory = Core::loadModel("Category")->getCategory([
            'where'=>['id'=>$list->cateid],
            "field"=>'id,title,parentid'
        ]);

        if($listCategory[0]){

            $parentCategory = Core::loadModel("Category")->getColumnValue(['id'=>$listCategory[0]->parentid],"title");

            $bread.='<li class=active>
                        <i class="wpcom-icon wi">
                          <svg aria-hidden=true>
                            <use xlink:href=#wi-arrow-right-3></use>
                          </svg>
                        </i>
                        <a href="'.url('index/product').'">'.$parentCategory.'</a>
                      </li>';
            $bread.='<li class=active>
                        <i class="wpcom-icon wi">
                          <svg aria-hidden=true>
                            <use xlink:href=#wi-arrow-right-3></use>
                          </svg>
                        </i>
                        <a href="'.url('index/product',['cid'=>$list->cateid]).'">'.$listCategory[0]->title.'</a>
                      </li>';
        }

        return $this->fetch("product_detail",['list'=>$list,'bread'=>$bread]);
    }
    public function news(){
        $cid = isset($this->param['cid'])?$this->param['cid']:7;

        // 通过父id查询所有子ID，包括后代的后代
        $ids = Core::loadModel('Category')->getChildIds($cid,true);

        $where = ["cateid"=>$ids,'isdelete'=>0,'ischeck'=>1];

        $this->seo_title = '新闻中心';
        $this->seo_keywords = '公司动态,行业新闻,设计新闻';

        $list = Core::loadModel("Posts")->getTpfPosts([
            "where"=>$where,
            "field"=>"id,title,thumb,keywords,content,datetime",
            "paginate"  =>['rows' =>12],
        ]);
        return $this->fetch("news",["list"=>$list]);
    }
    public function news_detail(){
        if(empty($this->param['id'])){
            $this->jump([RESULT_ERROR,'非法访问']);
        }

        $list = Core::loadModel("Posts")->getOneObject(["id"=>$this->param['id'],"isdelete"=>0,"ischeck"=>1],"id,title,abstract,keywords,content,parentid,cateid,datetime");

        if($list){
            $this->seo_title = $list->title;
            $this->seo_keywords = $list->keywords;
            $this->seo_description = $list->abstract;
        }else{
            $this->jump([RESULT_ERROR,'文章不存在']);
        }

        return $this->fetch("news_detail",['list'=>$list]);
    }
    public function contact(){
        $this->seo_title = '联系我们';
        return $this->fetch("contact");   
    }
    public function cases()
    {
        return $this->fetch("cases",[
            "list"=>Core::loadModel("Posts")->listPosts($this->param,3)
        ]);
    }
    public function about()
    {
        $this->seo_title = '关于我们';
        return $this->fetch("about");
    }
    public function changlang(){
       Cookie::set("think_var",$this->param['lang']);
    }
}
